package cn.gx.kevin.Utils;

import cn.gx.kevin.common.bootenv.SpringContextUtil;
import cn.gx.kevin.common.thread.BaseTask;
import cn.gx.kevin.common.user.LoginUser;
import cn.gx.kevin.common.utils.UrlInfo;
import cn.gx.kevin.domain.BaseLog;
import cn.gx.kevin.domain.Func;
import cn.gx.kevin.domain.Menu;
import cn.gx.kevin.service.IFuncService;
import cn.gx.kevin.service.IBaseLogService;
import cn.gx.kevin.service.IMenuService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.util.*;

/**
 * 写日志任务
 **/
public class LogTasks extends BaseTask {

    private static IBaseLogService logService = null;
    private static IMenuService menuService = null;
    private static IFuncService funcService = null;
    private static List<String> selectFields = new ArrayList<>();

    private static List<String> selectFunsFields = new ArrayList<>();

    public final Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());

    private Object runParams;

    public LogTasks(Object params) {
        this.runParams = params;
    }


    @Override
    public void setRunableParams(Object params) {
        this.runParams = params;
    }

    @Override
    public Object getRunableParams() {
        return this.runParams;
    }

    @Override
    public void run() {
        try {
            if (logService == null) {
                logService = (IBaseLogService) SpringContextUtil.getBean("baseLogService");
            }
            LogParams logParams = (LogParams) this.runParams;
            UrlInfo info = logParams.getUrlInfo();
            LoginUser user = logParams.getUser();
            String desp = logParams.getDesp();
            String menuTxt = "-";
            BaseLog log = new BaseLog();
            if (StringUtils.isEmpty(desp)) {
                if (menuService == null) {
                    selectFields.add("id");
                    selectFields.add("menu_text as menuText");
                    selectFunsFields.add("func_desc as funcDesc");
                    menuService = (IMenuService) SpringContextUtil.getBean("menuService");
                    funcService = (IFuncService) SpringContextUtil.getBean("funcService");
                }
                String controller = info.getController().replaceFirst("/", "");
                Map<String, Object> queryParams = new HashMap<>();
                queryParams.put("controller", controller);
                queryParams.put("selectFields", selectFields);
                List<Menu> menus = menuService.selectOnfield(queryParams);
                if (menus.size() > 0) {
                    String menuId = menus.get(0).getId();
                    menuTxt = menus.get(0).getMenuText();
                    queryParams.clear();
                    queryParams.put("menuId", menuId);
                    queryParams.put("selectFields", selectFunsFields);
                    queryParams.put("funcCode", info.getCmd());
                    List<Func> funcs = funcService.selectOnfield(queryParams);
                    if (funcs.size() > 0) {
                        desp = funcs.get(0).getFuncDesc();
                    }
                }
            }
            log.setContent(desp);
            log.setCreateTime(new Date());
            log.setLoginUser(user.getUserName());
            log.setOperateIp(user.getLoginIp());
            Object data = logParams.getData();
            if (data != null) {
                if (data instanceof String) {
                    log.setOperateData(String.valueOf(data));
                } else {
                    log.setOperateData(JSONObject.toJSONString(data));
                }
            }
            log.setUrl(info.getUrl());
            log.setMenuName(menuTxt);
            logService.insert(log);
        } catch (Exception ex) {
            logger.error("", ex);
        }

    }


}
